package com.zhongjian.technologycal.mall.activity.main.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.activity.personal.activity.MallOrderListActivity;
import com.zhongjian.technologycal.mall.alipay.AuthResult;
import com.zhongjian.technologycal.mall.alipay.PayResult;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.StatusBarUtil;
import com.zhongjian.technologycal.mall.utils.ToastUitl;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by wei on 2017/12/22.
 * 订单详情页面的实现类
 */

public class PayOrderItemsActivity extends BaseActivity {
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    private String singData;

    private static final int SDK_PAY_FLAG = 1;
    private static final int SDK_AUTH_FLAG = 2;
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult(msg.obj.toString());
                    /**
                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                        ToastUitl.showShort("支付成功");
                        finish();
                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                        ToastUitl.showShort("支付取消");
                    }
                    break;
                }
                case SDK_AUTH_FLAG: {
                    @SuppressWarnings("unchecked")
                    AuthResult authResult = new AuthResult((Map<String, String>) msg.obj, true);
                    String resultStatus = authResult.getResultStatus();

                    // 判断resultStatus 为“9000”且result_code
                    // 为“200”则代表授权成功，具体状态码代表含义可参考授权接口文档
                    if (TextUtils.equals(resultStatus, "9000") && TextUtils.equals(authResult.getResultCode(), "200")) {
                        // 获取alipay_open_id，调支付时作为参数extern_token 的value
                        // 传入，则支付账户为该授权账户
                        Toast.makeText(PayOrderItemsActivity.this,
                                "授权成功\n" + String.format("authCode:%s", authResult.getAuthCode()), Toast.LENGTH_SHORT)
                                .show();
                        ToastUitl.showShort("授权成功");
                    } else {
                        // 其他状态值则为授权失败
                        ToastUitl.showShort("授权失败");

                    }
                    break;
                }
                default:
                    break;
            }
        }
    };


    @Override
    protected void beforeInit() {
        super.beforeInit();
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);
    }
    @Override
    public void initPresenter() {

    }
    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_main_title.setText("订单支付");
        singData = getIntent().getStringExtra("singData");

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_pay_shopping_order;
    }

    @OnClick(R.id.iv_back_page)
    public void onClickBack(){
        Intent intent = new Intent();
        intent.setClass(getBaseContext(), MallOrderListActivity.class);
        intent.putExtra("TABSTUTUS", 0);
        startActivity(intent);
        finish();
    }

    /**
     *
     */
    @OnClick(R.id.pay_order_tv_commit)
    public void onClickCommitPaymentOrder(){
        if(!NetWorkUtils.isNetConnected(getBaseContext())){
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        Runnable payRunnable = new Runnable() {
            @Override
            public void run() {
                // 构造PayTask 对象
                PayTask alipay = new PayTask(
                        PayOrderItemsActivity.this);
                // 调用支付接口，获取支付结果
                String result = alipay.pay(singData, true);
                Message msg = new Message();
                msg.what = SDK_PAY_FLAG;
                msg.obj = result;
                mHandler.sendMessage(msg);
            }
        };
        // 必须异步调用
        Thread payThread = new Thread(payRunnable);
        payThread.start();


    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Intent intent = new Intent();
        intent.setClass(getBaseContext(), MallOrderListActivity.class);
        intent.putExtra("TABSTUTUS", 0);
        startActivity(intent);
        finish();
        return super.onKeyDown(keyCode, event);
    }
}
